<template>
  <div class="page-layout">
    <div style="margin-bottom: 12px">
      <el-button type="primary" size="mini" @click="add">新增</el-button>
    </div>
    <el-table border size="mini" ref="table" :data="tableData" :height="tableHeight" :style="{ width: '100%' }">
      <el-table-column prop="insuredName" label="被保险人名称">
      </el-table-column>
      <el-table-column prop="insuredCertificateType" label="证件类型">
        <template slot-scope="scope">
          <span>{{ scope.insuredCertificateType == '1' ? "身份证" : "统一社会信用代码" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="insuredCertificateNumber" label="证件号码"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="800" v-loading="loading">
      <el-form :model="ruleForm" ref="ruleForm" label-width="120px">
        <el-form-item label="被保险人名称" prop="insuredName" :rules="[{ required: true, message: '请输入投保人名称' }]">
          <el-input placeholder="请输入投保人名称" v-model="ruleForm.insuredName"></el-input>
        </el-form-item>
        <el-form-item label="证件类型" prop="insuredCertificateType">
          <el-select v-model="ruleForm.insuredCertificateType" placeholder="请选择证件类型">
            <el-option v-for="item in documentType" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证件号码" prop="insuredCertificateNumber" :rules="[{ required: true, message: '请输入证件号码' }]">
          <el-input placeholder="请输入证件号码" v-model="ruleForm.insuredCertificateNumber"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { documentType } from "@/utils/options";
// const reg =
//     /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i;
export default {
  computed: {
    hasInsureId() {
      return !!this.insuredId || this.insuredId === 0;
    },
  },
  data() {
    return {
      loading: false,
      insuredId: "",
      title: "新增银行账户",
      documentType,
      areaList: [],
      bankList: [],
      bankBranchList: [],
      ruleForm: {
        insuredName: "",
        insuredCertificateType: "2",
        insuredCertificateNumber: "",
      },
      dialogVisible: false,
      tableHeight: 0,
      tableData: [],
    };
  },
  methods: {
    add() {
      this.title = "新增被保险人";
      this.dialogVisible = true;
      this.insuredId = "";
      this.ruleForm = {
        insuredName: "",
        insuredCertificateType: "2",
        insuredCertificateNumber: "",
      };
    },
    edit(row) {
      this.title = "编辑被保险人";
      this.dialogVisible = true;
      this.insuredId = row.insuredId;
      this.ruleForm = {
        insuredName: row.insuredName,
        insuredCertificateType: row.insuredCertificateType,
        insuredCertificateNumber: row.insuredCertificateNumber,
      };
    },
    del(row) {
      this.$toConfirm("是否确认删除", "warning", "提示")
        .then(() => {
          this.delConfirm(row);
        })
        .catch(() => {
          //
        });
    },
    delConfirm(row) {
      this.$post("/config/insured/remove", {
        insuredId: row.insuredId,
      }).then((res) => {
        if (res && res.code * 1 === 200) {
          this.$message.success("删除成功");
          this.getTableData();
        }
      });
    },

    onSubmit() {
      const { insuredName, insuredCertificateType, insuredCertificateNumber } =
        this.ruleForm;
      const params = {
        insuredName,
        insuredCertificateType,
        insuredCertificateNumber,
      };
      const url = this.hasInsureId
        ? "/config/insured/update"
        : "/config/insured/add";
      if (this.hasInsureId) {
        params.insuredId = this.insuredId;
      }
      const message = this.hasInsureId ? "编辑成功" : "新增成功";
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          this.$post(url, params)
            .then(() => {
              this.$message.success(message);
              this.dialogVisible = false;
              if (this.$route.query.insureInfoId) {
                this.$router.push(
                  `/temp1?insureInfoId=${this.$route.query.insureInfoId}&templateName=${this.$route.query.templateName}&getInsuredDataFromCache=true`
                );
              }
              this.getTableData();
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    setTableHeight() {
      this.$nextTick(() => {
        const bodyHeight = window.innerHeight;
        this.tableHeight = bodyHeight - 185 + "px";
      });
    },
    getTableData() {
      this.$get("/config/insured/list").then((res) => {
        if (res) {
          this.tableData = res.data;
        } else {
          this.tableData = [];
        }
      });
    },
  },
  mounted() {
    this.setTableHeight();
    window.addEventListener("resize", this.setTableHeight);
    this.getTableData();
    if (this.$route.query.insureInfoId) {
      this.dialogVisible = true;
    }
  },
};
</script>
<style scoped lang="less">
.time {
  /deep/.el-form-item__label {
    line-height: 20px;
  }
}
</style>